define([], function() {
  var chameleonService ={

    browser: {
      versions: function() {
        var u = navigator.userAgent,
          app = navigator.appVersion;
        return { //移动终端浏览器版本信息
          trident: u.indexOf('Trident') > -1, //IE内核
          presto: u.indexOf('Presto') > -1, //opera内核
          webKit: u.indexOf('AppleWebKit') > -1, //苹果、谷歌内核
          gecko: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1, //火狐内核
          mobile: !!u.match(/AppleWebKit.*Mobile.*/), //是否为移动终端
          ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端
          android: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1, //android终端或uc浏览器
          iPhone: u.indexOf('iPhone') > -1, //是否为iPhone或者QQHD浏览器
          iPad: u.indexOf('iPad') > -1, //是否iPad
          webApp: u.indexOf('Safari') == -1, //是否web应该程序，没有头部与底部
          chameleon: u.indexOf('ChameleonAPP') > -1 //是否变色龙壳
        };
      }(),
      language: (navigator.browserLanguage || navigator.language).toLowerCase()
    },

    isReady:false,

    init:function(callback){
      var _this = this;
      console.log('ready function');
      document.addEventListener("deviceready", chameleonService.ready, false);
      return this
    },

    ready:function(){
      console.log('ready')
      chameleonService.isReady = true;
      // callback();
      // alert('ready')
    },

    exec:function(success,error,pluginID,methodName,arguments){
      // $('#isReady').html(chameleonService.isReady)
      var info = this.browser.versions
      var msg = JSON.stringify(info)
      // alert(navigator.userAgent)
      // alert(msg)
      if(chameleonService.isReady){
        console.log('...')
        cordova.exec(success,error,pluginID,methodName,arguments);
      }else if(!info.chameleon){
        try{
        if(typeof window.$ === "function"){
          var url = 'mock/'+pluginID+'/'+methodName+'.json';
          var successFunc = function(data){
            // console.log(data)
            success(data)
          }
          var errorFunc = function (a,b,c) {
            // console.dir(b)
            console.log(a,b,c);
            // console.log(b+" "+a.status+" : "+c);
            var msg = b+" "+a.status+" : "+c
            // alert(msg)
            error(msg)
          }
          // $.getJSON(url,successFunc,errorFunc);
          $.ajax({
            dataType: "text",
            url: url,
            success: successFunc,
            error: errorFunc
          });
        }
        }catch(e){alert(e)}
      }
    }
  };

  return chameleonService.init();
});
